package com.taotao.service.impl;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.taotao.pojo.TbUser;
import com.taotao.service.MQService;

@Service
public class MQServiceImpl implements MQService {

	/**
	 * 注入jms的模板，用于收发消息
	 */
	@Resource
	private JmsTemplate jmsTemplate;

	/**
	 * 注入队列的目的地
	 */
	@Resource
	private Destination queueDestination;

	@Override
	public void testSendMessage(Object obj) {
		jmsTemplate.send(queueDestination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage mapMessage = session.createMapMessage();
				if (obj instanceof TbUser) {
					TbUser tbUser = (TbUser) obj;
					mapMessage.setString("message", tbUser.getUsername() + "-----登陆了");
					mapMessage.setString("status", "success");
				} else {
					mapMessage.setString("message", "生产者发生了异常，传入数据类型错误");
					mapMessage.setString("status", "fail");
				}
				return mapMessage;
			}
		});
	}

}
